﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace Master
{
    /************************************
     * TRAFFIC LIGHT STRATEGY INTERFACE *
     ************************************/
    public interface ITrafficLightStrategy
    {
        void start();
        void stop();
    }

    /**********************************
     * TRAFFIC LIGHT STRATEGY FACTORY *
     **********************************/
    public class TrafficLightStrategyFactory
    {
        public ITrafficLightStrategy createSimpleStrategy()
        {
            return new SimpleTrafficLightStrategy();
        }
    }

    /******************************************
     * TRAFFIC LIGHT STRATEGY IMPLEMENTATIONS *
     ******************************************/
    class SimpleTrafficLightStrategy : ITrafficLightStrategy
    {
        private Thread strategyThread;

        public void start()
        {
            strategyThread = new Thread(handleTraffic);
            strategyThread.Start();
        }

        public void stop()
        {
            strategyThread.Abort();
        }

        private void handleTraffic()
        {
            try
            {
                while (true)
                {
                    //*********** HANDLE TRAFFIC LIGHTS HERE
                    Trace.WriteLine("looping..");
                    Thread.Sleep(500);
                }
            }
            catch (ThreadAbortException e)
            {
                Trace.WriteLine("Thread was aborted by: " + e.GetType());
                return;
            }
        }
    }
}
